<template>
    <div class="wrapper">
        <header>
        <!-- 跳转设置 -->
        <li @click="goToHospital">
            <img class="icon" :src="'src/img/left.png'" alt="返回图标"> 
        </li>


            <i class="fa fa-angle-left" @click="goBack"></i>
            <p>选择体检套餐!</p>
            <div></div>
        </header>
        <div class="top-ban"></div>

        <ul class="setmeal" v-if="setmealList.length">
            <li v-for="(item, index) in uniqueSetmealNames" :key="index">
                <div class="item">
                    <div class="item-left">
                        <h3 @click="selectSetmealAndGo(item)"> <!-- 添加点击事件处理选择套餐逻辑 -->
                            {{ item }}
                        </h3>
                        <div class="item-down" @click="gotoSelectDate">
                            <h3>{{ modifySetName(item) }}</h3>
                        </div>
                    </div>
                    <div class="item-right" @click="toggleDetails(item)">
                        <p>详情 ></p>
                        <i class="fa fa-angle-down" :class="{ active: isDetailsOpen[item] }"></i>
                    </div>
                </div>
                <div class="item-content" v-if="isDetailsOpen[item]">
                    <table>
                        <tr>
                            <th>检查项目</th>
                            <th>检查内容</th>
                            <th>检查意义</th>
                        </tr>
                        <tr v-for="detail in setmealList.filter(s => s.name === item)" :key="detail.smId">
                            <td>{{ detail.ciName }}</td>
                            <td>{{ detail.ciContent }}</td>
                            <td>{{ detail.meaning }}</td>
                        </tr>
                    </table>
                </div>
            </li>
        </ul>

        <div class="bottom-ban"></div>
    </div>
    <Footer></Footer>
</template>

<script>
import Footer from "@/components/Footer.vue";
import axios from "axios";
import { getSessionStorageItem, setSessionStorage } from '../common.js'; // 导入 setSessionStorage


export default {
    components: {
        Footer,
    },
    data() {
        return {
            setmealList: [], // 存储体检套餐列表数据
            isDetailsOpen: {}, // 存储套餐详情是否展开的状态
            selectedSetmealId: null, // 选定的套餐ID
        };
    },
    async created() {
        const userId = getSessionStorageItem('user')?.userId; // 从 sessionStorage 获取用户ID
        const hospitalId = getSessionStorageItem('selectedHospitalId'); // 从 sessionStorage 获取选定的医院ID
        console.log(hospitalId);

        if (userId && hospitalId) {
            try {
                const response = await axios.get("http://localhost:8080/setmeal/list", { params: { userId, hospitalId } }); // 发送获取体检套餐列表的请求
                this.setmealList = response.data.data; // 更新体检套餐列表数据

                // 初始化套餐详情展开状态
                this.setmealList.forEach(item => {
                    this.isDetailsOpen[item.name] = false;
                });
            } catch (error) {
                console.error("获取体检套餐列表失败", error); // 输出错误信息到控制台
            }
        } else {
            console.error("未在sessionStorage中找到用户ID或医院ID");
        }
    },
    computed: {
        uniqueSetmealNames() {
            return [...new Set(this.setmealList.map(item => item.name))]; // 计算出所有唯一的套餐名称
        },
    },
    methods: {
        toggleDetails(name) {
            this.isDetailsOpen[name] = !this.isDetailsOpen[name]; // 切换指定套餐名称的详情展开状态
        },

        goBack() {
            window.history.go(-1); // 返回上一页
        },

        modifySetName(originalName) {
            const parts = originalName.split('-');
            if (parts.length >= 2) {
                return `普通${parts[0]}客户-${parts.slice(1).join('-')}`; // 修改套餐名称的显示格式
            }
            return originalName;
        },

        async selectSetmealAndGo(item) {
            const selectedSetmeal = this.setmealList.find(s => s.name === item); // 查找选定的套餐对象
            if (selectedSetmeal) {
                this.selectedSetmealId = selectedSetmeal.smId; // 设置选定的套餐ID

                // 确保selectedSetmealId已设置后再执行后续操作
                await new Promise(resolve => setTimeout(resolve, 0)); // 强制进入微任务队列，确保数据更新后再执行

                setSessionStorage('selectedSetmealId', selectedSetmeal.smId); // 将选定的套餐ID存储到 sessionStorage
                console.log(selectedSetmeal.smId);

                if (this.selectedSetmealId) {
                    this.$router.push({ path: '/doc' }); // 跳转到选择日期页面
                } else {
                    console.error('No setmeal selected.');
                }
            } else {
                console.error('套餐未找到:', item);
            }
        },

        goToHospital() {
            this.$router.push('/hospital'); // 跳转到医院页面
        }
    },
};
</script>



<style scoped>
/*********************** 总容器 ***********************/
.wrapper {
    width: 100%;
    height: 100%;
    background-color: #F9F9F9;
}

/*********************** header ***********************/
/* 返回 */
header li {
list-style-type: none;
}
.icon{
float: left;

width: 80%;
height: 8.2vw;
}



header {
    width: 100%;
    height: 15.7vw;
    background-color: #FFF;

    position: fixed;
    left: 0;
    top: 0;

    display: flex;
    align-items: center;
    justify-content: space-between;

    box-sizing: border-box;
    padding: 0 3.6vw;
}

header .fa {
    font-size: 8vw;
}

/*********************** footer ***********************/
footer {
    width: 100%;
    height: 14.2vw;
    box-sizing: border-box;
    border-top: solid 1px #E9E9E9;
    background-color: #FFF;

    position: fixed;
    left: 0;
    bottom: 0;
}

footer ul {
    width: 100%;
    height: 14.2vw;
    display: flex;
    align-items: center;
    justify-content: space-around;
}

footer ul li {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    font-size: 3vw;
    color: #999;

    user-select: none;
    cursor: pointer;
}

footer ul li .fa {
    font-size: 5vw;
}

/*********************** common样式 ***********************/
.top-ban {
    width: 100%;
    height: 15.7vw;
}

.bottom-ban {
    width: 100%;
    height: 14.2vw;
}

/*********************** setmeal ***********************/
.setmeal {
    width: 100%;
    margin-top: 10vw;
}

.setmeal li {
    width: 92.8vw;
    margin: 0 auto;
    border: solid 1px #EEE;
    border-radius: 1vw;
    box-shadow: 2px 2px 5px rgba(0, 0, 0, .08);
    background-color: #FFF;
    margin-bottom: 3.6vw;
}

.setmeal li .item {
    width: 100%;
    height: 20vw;
    color: #555;

    display: flex;
    align-items: center;
}

.setmeal li .item .item-left {
    flex: 0 0 72vw;
    height: 20vw;
    border-right: solid 1px #EEE;
    box-sizing: border-box;
    padding-left: 12vw;

    display: flex;
    flex-direction: column;
    justify-content: center;

    user-select: none;
    cursor: pointer;
}

.setmeal li .item .item-down {


    display: flex;
    flex-direction: column;
    justify-content: center;
    /* 添加的样式 */
    font-size: 5px;
    /* 调整字体大小 */
    color: #999;
    /* 设置淡色字体颜色 */
}

.setmeal li .item .item-left h3 {
    font-size: 4.3vw;
    font-weight: 600;
}

.setmeal li .item .item-left p {
    font-size: 4vw;
    margin-top: 1vw;
}

.setmeal li .item .item-right {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;

    user-select: none;
    cursor: pointer;
}

.setmeal li .item .item-right p {
    font-size: 4vw;
    margin-right: 2vw;
}

.setmeal li .item-content {
    /*display: none;*/
    width: 100%;
    background-color: #FFF;
}

.setmeal li .item-content table {
    width: 100%;
    border-collapse: collapse;
    font-size: 4vw;
    color: #555;
}

.setmeal li .item-content table tr {
    display: flex;
}

.setmeal li .item-content table tr td,
.setmeal li .item-content table tr th {
    flex: 1;
}

.setmeal li .item-content table tr th {
    text-align: center;
    background-color: #EEE;
    height: 10vw;
    line-height: 10vw;
}

.setmeal li .item-content table tr td {
    border: solid 1px #EFEFEF;
    box-sizing: border-box;
    padding: 2vw;
}
</style>